/*
    Copyright 2020-2021. Huawei Technologies Co., Ltd. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License")
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:huawei_ml/imgseg/ml_image_segmentation_analyzer_setting.dart';
import 'package:huawei_ml/models/ml_image_segmentation.dart';
import 'package:huawei_ml/utils/channels.dart';

class MLImageSegmentationAnalyzer {
  static const int TYPE_BACKGROUND = 0;
  static const int TYPE_HUMAN = 1;
  static const int TYPE_SKY = 2;
  static const int TYPE_GRASS = 3;
  static const int TYPE_FOOD = 4;
  static const int TYPE_CAT = 5;
  static const int TYPE_BUILD = 6;
  static const int TYPE_FLOWER = 7;
  static const int TYPE_WATER = 8;
  static const int TYPE_SAND = 9;
  static const int TYPE_MOUNTAIN = 10;

  final MethodChannel _channel = Channels.segmentationMethodChannel;

  Future<MLImageSegmentation> asyncAnalyzeFrame(MLImageSegmentationAnalyzerSetting setting) async {
    return new MLImageSegmentation.fromJson(json.decode(await _channel.invokeMethod("asyncAnalyzeFrame", setting.toMap())));
  }

  Future<List<MLImageSegmentation>> analyzeFrame(MLImageSegmentationAnalyzerSetting setting) async {
    var res = json.decode(await _channel.invokeMethod("analyzeFrame", setting.toMap()));
    return (res as List).map((e) => MLImageSegmentation.fromJson(e)).toList();
  }

  Future<bool> stopSegmentation() async {
    return await _channel.invokeMethod("stopSegmentation");
  }
}
